package org.microservice.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.microservice.common.bean.RoleVo;
import org.microservice.common.util.AuthUtil;
import org.microservice.common.util.R;
import org.microservice.system.dao.SysRoleDao;
import org.microservice.system.service.SysRoleMenuService;
import org.microservice.system.service.SysRoleService;
import org.microservice.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/6/20
 * Time: 15:51
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao,RoleVo> implements SysRoleService{

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;


    @Override
    public List<RoleVo> getAllRoles() {
        return sysRoleDao.selectList(new EntityWrapper<RoleVo>());
    }

    @Override
    public List<RoleVo> getUserRoleByUserId(int userId) {

        List<RoleVo> roleVos= sysRoleDao.getUserRoleByUserId(userId);

        return null;
    }

    @Override
    public List<RoleVo> getRoleListPage(Map<String, Object> params) {

        params.put("minWeight",setMinWeight());
        return sysRoleDao.getRoleListPage(params);
    }

    @Override
    public int getRoleListPageTotal(Map<String, Object> params) {
        params.put("minWeight",setMinWeight());
        return sysRoleDao.getRoleListPageTotal(params);
    }


    private int setMinWeight(){
        List<RoleVo> roleVos = getRolesByUserName(AuthUtil.getUserName());
        int minWeight = roleVos.get(0).getWeight();
        if(roleVos.size() > 1){
            for (RoleVo role :
                    roleVos) {
                if (role.getWeight() < minWeight) {
                    minWeight = role.getWeight();
                }
            }
        }

        return minWeight;

    }



    @Override
    @Transactional
    public R deleteRole(Serializable roleId) {
        Map map = new HashMap<String,Integer>();
        map.put("role_id",roleId);
        boolean b = sysUserRoleService.deleteByMap(map);

        if(b){
            boolean b1 = sysRoleMenuService.deleteByMap(map);
            if(b1){
                Integer i = sysRoleDao.deleteById(Integer.parseInt(roleId.toString()));
                if(i>0){
                    return R.ok();
                }
            }

        }

        return R.error();
    }

    @Override
    public R assignMenus(RoleVo roleVo) {
        sysRoleMenuService.saveOrUpdate(roleVo.getId(),roleVo.getMenuIds());
        return R.ok();
    }

    @Override
    public List<RoleVo> getRolesByUserName(String userName) {
        return sysRoleDao.getRolesByUserName(userName);
    }
}
